<?php

class Agen extends CI_Controller {
	
	function Agen()
	{
		parent::__construct();
		$this->load->model('agen_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		
		// Load default view
		$data['title'] = "Agen";
		$data['main_view'] = 'agen/agen_view';
		
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['all_kota'] = $this->lokasi_model->get_all_kota();
		
		$all_agen = $this->agen_model->get_all_agen();
		$data['all_agen'] = $all_agen->result_array();
		$all_kordinat = $this->agen_model->get_all_kordinat();
		$data['all_kordinat'] = $all_kordinat->result_array();
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function add_agen(){
	
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$id_titik_pasok = $this->agen_model->insert_titik_pasok($arr);
		
		$arr2 = array(
			'ID_TITIK_PASOK' => $id_titik_pasok,
			'HARGA_JUAL' => $this->input->post('harga_jual'),
			'NO_PERIJINAN' => $this->input->post('no_perijinan')
		);
		
		$this->agen_model->insert_agen($arr2);
		
	}
	
	function update_agen(){
		$id = $this->input->post('ID_TITIK_PASOK');
		
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		if(count($tanggal_arr) > 1) $tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr1 = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$agen = $this->agen_model->get_agen($id);
		
		$arr2 = array(
			'HARGA_JUAL' => $this->input->post('harga_jual'),
			'NO_PERIJINAN' => $this->input->post('no_perijinan')
		);
		
		//var_dump($arr1);
		$this->agen_model->update_agen($id, $arr1, $arr2);
		
	}
	
	function delete_agen(){
		$id_pasok = $this->input->post('id_titik_pasok');
		$this->agen_model->delete_agen($id_pasok);
	}	
	
}